﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Text;
using System.Xml;

namespace PhotoList.Dal
{
    public class DBO : System.Web.UI.Page
    {
        private static string xPath = ConfigurationManager.AppSettings["DataBasePath"].ToString();

        #region User

        public struct UserStruct
        {
            public string uid;
            public string pass;
            public string home;
            public string name;
            public string group;
        }

        #region Load

        /// <summary>
        /// LoadUser
        /// </summary>
        /// <param name="uID">UserID</param>
        /// <returns>UserStruct</returns>
        public static UserStruct LoadUser(string uID)
        {
            UserStruct us;
            us.uid = "";
            us.pass = "";
            us.home = "";
            us.name = "";
            us.group = "";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList").SelectNodes("Record");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;
                if (xe.GetAttribute("userid").ToUpper() == uID.ToUpper())
                {
                    us.uid = xe.GetAttribute("userid");
                    us.pass = xe.SelectSingleNode("pass").InnerText;
                    us.home = xe.SelectSingleNode("home").InnerText;
                    us.name = xe.GetAttribute("name");
                    us.group = xe.GetAttribute("group");
                }
            }

            return us;
        }

        public static UserStruct[] LoadUser()
        {
            UserStruct[] us = null;
            int i = 0;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList").SelectNodes("Record");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;
                us[i].uid = xe.GetAttribute("userid");
                us[i].pass = xe.SelectSingleNode("pass").InnerText;
                us[i].home = xe.SelectSingleNode("home").InnerText;

                i++;
            }

            return us;
        }

        public static DataTable LoadUserList()
        {
            int i = 1;

            DataTable dt = new DataTable();
            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns[0].AutoIncrement = true;
            dt.Columns[0].AutoIncrementSeed = 1;
            dt.Columns[0].AutoIncrementStep = 1;
            dt.Columns[0].AllowDBNull = false;
            dt.Columns.Add("userid", Type.GetType("System.String"));
            dt.Columns[1].AllowDBNull = false;
            dt.Columns.Add("pass", Type.GetType("System.String"));
            dt.Columns[1].AllowDBNull = false;
            dt.Columns.Add("home", Type.GetType("System.String"));
            dt.Columns[1].AllowDBNull = false;
            dt.Columns.Add("name", Type.GetType("System.String"));
            dt.Columns[1].AllowDBNull = true;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList").SelectNodes("Record");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;

                dt.Rows.Add(new object[] { i, xe.GetAttribute("userid"),xe.SelectSingleNode("pass").InnerText,xe.SelectSingleNode("home").InnerText,xe.GetAttribute("name") });

                i++;
            }

            return dt;
        }

        #endregion

        #region Add

        public static bool AddUser(string uID, string pass, string home, string name, string group)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));
                XmlNode UserList = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList");

                XmlElement Record=xmlDoc.CreateElement("Record");
                Record.SetAttribute("userid", uID);
                Record.SetAttribute("group",group);
                Record.SetAttribute("name", name);
                XmlElement Pass=xmlDoc.CreateElement("pass");
                Pass.InnerText = pass;
                Record.AppendChild(Pass);
                XmlElement Home = xmlDoc.CreateElement("home");
                Home.InnerText = home;
                Record.AppendChild(Home);
                UserList.AppendChild(Record);
                xmlDoc.Save(new System.Web.UI.Page().Server.MapPath(xPath));
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion

        #region Modify

        public static bool ModiUser(string uID, string pass, string home, string name, string group)
        {
            try
            {
                Update("/ROOT/UserList/Record[@userid='" + uID + "']", "name", name);
                Update("/ROOT/UserList/Record[@userid='" + uID + "']", "group", group);
                Update("/ROOT/UserList/Record[@userid='" + uID + "']/pass", "", pass);
                Update("/ROOT/UserList/Record[@userid='" + uID + "']/home", "", home);
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion

        #region Delete

        public static bool DeleUser(string uID)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

                XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList").SelectNodes("Record");

                foreach (XmlNode xmlNode in xmlNodes)
                {
                    XmlElement xe = (XmlElement)xmlNode;
                    if (xe.GetAttribute("userid").ToUpper() == uID.ToUpper())
                    {
                        xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("UserList").RemoveChild(xe);
                    }
                }

                xmlDoc.Save(new System.Web.UI.Page().Server.MapPath(xPath));
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion

        #endregion

        #region Function

        public struct FuncStruct
        {
            public string id;
            public string name;
            public string group;
            public string user;
            public string path;
        }

        public static FuncStruct LoadFuncs(string ID)
        {
            FuncStruct fs;
            fs.id = "";
            fs.name = "";
            fs.group = "";
            fs.user = "";
            fs.path = "";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("Menu").SelectNodes("Funcs");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;
                if (xe.GetAttribute("id").ToUpper() == ID.ToUpper())
                {
                    fs.id = xe.GetAttribute("id");
                    fs.name = xe.GetAttribute("name");
                    fs.group = xe.GetAttribute("group");
                    fs.user = xe.GetAttribute("user");
                    fs.path = xe.InnerText;
                }
            }

            return fs;
        }

        public static FuncStruct CheckFunc(string url)
        {
            FuncStruct fs;
            fs.id = "";
            fs.name = "";
            fs.group = "";
            fs.user = "";
            fs.path = "";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("Menu").SelectNodes("Funcs");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;
                if (xe.InnerText.ToUpper() == url.ToUpper())
                {
                    fs.id = xe.GetAttribute("id");
                    fs.name = xe.GetAttribute("name");
                    fs.group = xe.GetAttribute("group");
                    fs.user = xe.GetAttribute("user");
                    fs.path = xe.InnerText;
                }
            }

            return fs;
        }

        public static FuncStruct[] LoadFuncs()
        {
            FuncStruct[] fs = null;
            int i = 0;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new System.Web.UI.Page().Server.MapPath(xPath));

            XmlNodeList xmlNodes = xmlDoc.SelectSingleNode("ROOT").SelectSingleNode("Menu").SelectNodes("Funcs");

            foreach (XmlNode xmlNode in xmlNodes)
            {
                XmlElement xe = (XmlElement)xmlNode;
                fs[i].id = xe.GetAttribute("id");
                fs[i].name = xe.GetAttribute("name");
                fs[i].group = xe.GetAttribute("group");
                fs[i].user = xe.GetAttribute("user");
                fs[i].path = xe.InnerText;

                i++;
            }

            return fs;
        }

        #endregion

        public DBO()
        {
        }

        #region XML操作

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时返回该属性值，否则返回串联值</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read("/Node", "")
         * XmlHelper.Read("/Node/Element[@Attribute='Name']", "Attribute")
         ************************************************/
        public static string Read(string node, string attribute)
        {
            string value = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new System.Web.UI.Page().Server.MapPath(xPath));
                XmlNode xn = doc.SelectSingleNode(node);
                value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
            }
            catch { }
            return value;
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="element">元素名，非空时插入新元素，否则在该元素中插入属性</param>
        /// <param name="attribute">属性名，非空时插入该元素属性值，否则插入元素值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert("/Node", "Element", "", "Value")
         * XmlHelper.Insert("/Node", "Element", "Attribute", "Value")
         * XmlHelper.Insert("/Node", "", "Attribute", "Value")
         ************************************************/
        public static bool Insert(string node, string element, string attribute, string value)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new System.Web.UI.Page().Server.MapPath(xPath));
                XmlNode xn = doc.SelectSingleNode(node);
                if (element.Equals(""))
                {
                    if (!attribute.Equals(""))
                    {
                        XmlElement xe = (XmlElement)xn;
                        xe.SetAttribute(attribute, value);
                    }
                }
                else
                {
                    XmlElement xe = doc.CreateElement(element);
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    xn.AppendChild(xe);
                }
                doc.Save(new System.Web.UI.Page().Server.MapPath(xPath));
            }
            catch 
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时修改该节点属性值，否则修改节点值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert("/Node", "", "Value")
         * XmlHelper.Insert("/Node", "Attribute", "Value")
         ************************************************/
        public static bool Update(string node, string attribute, string value)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new System.Web.UI.Page().Server.MapPath(xPath));
                XmlNode xn = doc.SelectSingleNode(node);
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals(""))
                    xe.InnerText = value;
                else
                    xe.SetAttribute(attribute, value);
                doc.Save(new System.Web.UI.Page().Server.MapPath(xPath));
            }
            catch 
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时删除该节点属性值，否则删除节点值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Delete("/Node", "")
         * XmlHelper.Delete("/Node", "Attribute")
         ************************************************/
        public static bool Delete(string node, string attribute)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new System.Web.UI.Page().Server.MapPath(xPath));
                XmlNode xn = doc.SelectSingleNode(node);
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn);
                else xe.RemoveAttribute(attribute);
                doc.Save(new System.Web.UI.Page().Server.MapPath(xPath));
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion 
    }
}

